package edu.hit.pact.pgse.timer;

import edu.hit.pact.pgse.util.Globals;

/*
 * monitor that whether the coefficient of web, blog, bbs have changed
 * in order to 
 */
public class ConfMonitor {
	private int[] webFactor = new int[3];
	private int[] blogFactor = new int[3];
	private int[] bbsFactor = new int[3];
	private int confFactor;
	
	public ConfMonitor(){
		webFactor[0] = Globals.TIME_WEB_TASK_CRAWL_HOUR;
		webFactor[1] = Globals.TIME_WEB_TASK_CRAWL_MINUTE;
		webFactor[2] = Globals.INTERVAL_CRAWL_WEB_TASK;
		
		blogFactor[0] = Globals.TIME_BLOG_TASK_CRAWL_HOUR;
		blogFactor[1] = Globals.TIME_BLOG_TASK_CRAWL_MINUTE;
		blogFactor[2] = Globals.INTERVAL_CRAWL_BLOG_TASK;
		
		bbsFactor[0] = Globals.TIME_BBS_TASK_CRAWL_HOUR;
		bbsFactor[1] = Globals.TIME_BBS_TASK_CRAWL_MINUTE;
		bbsFactor[2] = Globals.INTERVAL_CRAWL_BBS_TASK;
		
		confFactor = Globals.INTERVAL_UPDATE_CONF_TASK;
	}
	
	public void update(){
		webFactor[0] = Globals.TIME_WEB_TASK_CRAWL_HOUR;
		webFactor[1] = Globals.TIME_WEB_TASK_CRAWL_MINUTE;
		webFactor[2] = Globals.INTERVAL_CRAWL_WEB_TASK;
		
		blogFactor[0] = Globals.TIME_BLOG_TASK_CRAWL_HOUR;
		blogFactor[1] = Globals.TIME_BLOG_TASK_CRAWL_MINUTE;
		blogFactor[2] = Globals.INTERVAL_CRAWL_BLOG_TASK;
		
		bbsFactor[0] = Globals.TIME_BBS_TASK_CRAWL_HOUR;
		bbsFactor[1] = Globals.TIME_BBS_TASK_CRAWL_MINUTE;
		bbsFactor[2] = Globals.INTERVAL_CRAWL_BBS_TASK;
		
		confFactor = Globals.INTERVAL_UPDATE_CONF_TASK;
	}
	
	public void applyCheck(ConfTaskScheduler taskScheduler){

		if ((webFactor[0] != Globals.TIME_WEB_TASK_CRAWL_HOUR) 
				|| (webFactor[1] != Globals.TIME_WEB_TASK_CRAWL_MINUTE) 
				|| (webFactor[2] != Globals.INTERVAL_CRAWL_WEB_TASK)){
			
			taskScheduler.reScheduleTask("web");
		}
		
		if ((blogFactor[0] != Globals.TIME_BLOG_TASK_CRAWL_HOUR) 
				|| (blogFactor[1] != Globals.TIME_BLOG_TASK_CRAWL_MINUTE) 
				|| (blogFactor[2] != Globals.INTERVAL_CRAWL_BLOG_TASK)){
			taskScheduler.reScheduleTask("blog");
		}

		if ((bbsFactor[0] != Globals.TIME_BBS_TASK_CRAWL_HOUR) 
				|| (bbsFactor[1] != Globals.TIME_BBS_TASK_CRAWL_MINUTE) 
				|| (bbsFactor[2] != Globals.INTERVAL_CRAWL_BBS_TASK)){
			taskScheduler.reScheduleTask("bbs");
		} 
		
		if (confFactor != Globals.INTERVAL_UPDATE_CONF_TASK){
			System.out.println("update interval changed");
			taskScheduler.reScheduleTask("conf");
		}
		
		update();
	}
}
